#include "../csapp.h"
#include "sbuf.h"

#define NTHREADS 4
#define SBUFSIZE 16

void ehco_cnt(int connfd);
void *thread(void *vargp);

sbuf_t sbuf;

int test_echoservert_pre(int argc, char **argv)
{
	int i, listenfd, connfd, port, clientlen = sizeof(struct sockaddr_in);
	struct sockaddr_in clientaddr;
	pthread_t tid;
	
	if (argc != 2)
	{
		fprintf(stderr, "usage: %s <port>\n", argv[0]);
		return 0;
	}
	
	port = atoi(argv[1]);
	sbuf_init(&sbuf, SBUFSIZE);
	listenfd = Open_listenfd(port);
	
	for (i = 0; i < NTHREADS; i++)
		Pthread_create(&tid, NULL, thread, NULL);
	
	while (1)
	{
		connfd = Accept(listenfd, (SA *) &clientaddr, (socklen_t *)&clientlen);
		sbuf_insert(&sbuf, connfd);
	}
}


void *thread(void *vargp)
{
	Pthread_detach(pthread_self());
	while (1)
	{
		int connfd = sbuf_remove(&sbuf);
		echo_cnt(connfd);
		Close(connfd);
	}
}
















